package com.xinzhi.savingpot.background.service.impl;

import com.xinzhi.savingpot.background.entity.User;
import com.xinzhi.savingpot.background.mapper.ISystemMapper;
import com.xinzhi.savingpot.background.service.ISystemService;
import com.xinzhi.savingpot.background.vo.UserVo;
import com.xinzhi.savingpot.enums.StatusCode;
import com.xinzhi.savingpot.util.BaseResponse;
import com.xinzhi.savingpot.util.MD5Util;
import com.xinzhi.savingpot.util.PageUtil;
import com.xinzhi.savingpot.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemServiceImpl implements ISystemService {
    @Autowired
    ISystemMapper iSystemMapper;//Dao层
    @Autowired
    RedisUtil redisUtil;//Redis工具类
    @Autowired
    PageUtil pageUtil;// 分页工具类
    // TODO 添加管理员
    @Override
    public BaseResponse addAdmin(UserVo userVo) throws Exception {

        if (userVo.getUsername()!=null&&userVo.getPassword()!=null&&userVo.getSuserrole()!=null){
            int countUserName = iSystemMapper.countUserName(userVo.getUsername());
            if (!(countUserName>0)){
                User user=new User();
                user.setUsername(userVo.getUsername());
                user.setPassword(MD5Util.stringMD5(userVo.getPassword()));
                user.setSuserrole(userVo.getSuserrole());
                int count = iSystemMapper.insertAdmin(user);
                if (count>0){
                    redisUtil.set("SystemUser:"+userVo.getUsername(),"");
                    return new BaseResponse(StatusCode.Success);
                }
                return new BaseResponse(-1,"添加失败");
            }
            return new BaseResponse(-1,"用户名已存在");
        }
        throw new Exception("请认真填写内容！！");
    }


    // TODO 查询系统用户
    @Override
    public BaseResponse querySystemUser() {
        List<User> userList = ( List<User>) redisUtil.get("SystemUser:All");
        if (userList == null) {
            List<User> users = iSystemMapper.querySystemUser();
            if (!users.isEmpty()) {
                redisUtil.set("SystemUser:All", users);
                return new BaseResponse(0, "查询成功", users);
            }
            return new BaseResponse(-1, "查询失败");
        }
        return new BaseResponse(0, "查询成功", userList);
    }


    // TODO 修改系统用户信息
    @Override
    public BaseResponse changeSystemUser(UserVo userVo) {
        if (userVo.getUsername()!=null&&(userVo.getPassword()!=null||userVo.getSuserrole()!=null)){
            User user=new User();
            user.setUsername(userVo.getUsername());
            user.setPassword(userVo.getPassword());
            user.setSuserrole(userVo.getSuserrole());
            redisUtil.del("SystemUser:"+userVo.getUsername());
            int count = iSystemMapper.changeSystemUser(user);
            if (count>0){
                redisUtil.del("SystemUser:"+userVo.getUsername());
                List<User> users = iSystemMapper.querySystemUser();
                for (User user1:users) {
                    redisUtil.set("SystemUser:"+user1.getUsername(),user1);
                }

                return new BaseResponse(0,"修改成功");
            }
            return new BaseResponse(-1,"修改失败");
        }
        return new BaseResponse(StatusCode.Fail);
    }

    // TODO 删除系统用户
    @Override
    public BaseResponse delSystemUser(String username) {
        int count = iSystemMapper.delSystemUser(username);
        if (count>0){
            return new BaseResponse(0,"删除成功");
        }
        return new BaseResponse(-1,"删除失败");
    }

    /**
     *
     * @param index
     * @param
     * @return
     */
    @Override
    public BaseResponse queryPage(String index) {

        int count = iSystemMapper.count(null);
        if (count>0){
            pageUtil.setTotalRecords(count);
            if (index!=null){
                int indexInt = Integer.parseInt(index);
                if (indexInt<1){
                    indexInt=1;
                }else if (indexInt>pageUtil.getMaxPages()){
                    indexInt=pageUtil.getMaxPages();
                }
                pageUtil.setPageIndex(indexInt);
            }
        pageUtil.setList(iSystemMapper.queryPage(((pageUtil.getPageIndex() - 1) * pageUtil.getPageRecords()) + 1, pageUtil.getPageRecords()));
            return new BaseResponse(0,"查询成功",pageUtil);
        }
        return new BaseResponse(-1,"查询失败");
    }

    @Override
    public BaseResponse queryGroup(String suserrole,String index) {
        User user = new User();
        user.setSuserrole(suserrole);
        int count = iSystemMapper.count(user.getUserroleid());
        if (count > 0) {
            pageUtil.setTotalRecords(count);
            if (index != null) {
                int indexInt = Integer.parseInt(index);
                if (indexInt < 1) {
                    indexInt = 1;
                } else if (indexInt > pageUtil.getMaxPages()) {
                    indexInt = pageUtil.getMaxPages();
                }
                pageUtil.setPageIndex(indexInt);
            }
            pageUtil.setList(iSystemMapper.queryGroup(user.getUserroleid(),((pageUtil.getPageIndex()  - 1) * pageUtil.getPageRecords()) , pageUtil.getPageRecords()));
            return new BaseResponse(0,"查询成功",pageUtil);
        }
        return new BaseResponse(0,"查询失败");
    }
}
